//
//  MJRefresh+Rx.swift
//  RXmvvmDemo
//
//  Created by jemi on 2019/12/27.
//  Copyright © 2019 jemi. All rights reserved.
//

import RxSwift
import RxCocoa

extension Reactive where Base: MJRefreshComponent {
    
    var refreshing : ControlEvent<Void> {
        let source : Observable<Void> = Observable.create {
            [weak control = self.base] observer in
            if let control = control {
                control.refreshingBlock = {
                    observer.onNext(())
                }
            }
            return Disposables.create()
        }
        return ControlEvent(events: source)
    }
    
    var endRefreshing : Binder<Bool> {
        return Binder(base) { (refresh, isEnd) in
            if isEnd {
                refresh.endRefreshing()
            }
        }
    }
}
